#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define BUFSIZE 1024

void error_handling(char *message) {
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

int main(int argc, char *argv[]) {
    int client_sock;
    if (argc != 3) {
        printf("Usage : %s <IP> <port>\n", argv[0]);
        exit(1);
    }
    if ((client_sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
        error_handling("socket() error!");
    }
    
    struct sockaddr_in y_addr, from_addr;
    memset(&y_addr, 0, sizeof(y_addr));
    y_addr.sin_family = AF_INET;
    y_addr.sin_addr.s_addr = inet_addr(argv[1]);
    y_addr.sin_port = htons(atoi(argv[2]));

    char ch;
    char buf1[] = "HI!";
    char buf2[] = "i am another UDP host!";
    char buf3[] = "Nice to meet you";
        // 分配端口
    sendto(client_sock, buf1, strlen(buf1), 0, (struct sockaddr *)&y_addr, sizeof(y_addr));
    sendto(client_sock, buf2, strlen(buf2), 0, (struct sockaddr *)&y_addr, sizeof(y_addr));
    sendto(client_sock, buf3, strlen(buf3), 0, (struct sockaddr *)&y_addr, sizeof(y_addr));
    
    close(client_sock);
    return 0;
}